package kr.co.handysoft.common.profiler;

import kr.co.handysoft.common.compress.Archiving;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class DiskCommand extends AbstractCommand{

	@Override
	public String invoke() {
	
		Sigar sigar = new Sigar();
		this.command = "DISK";
	
		JSONObject fileSystemInfo = new JSONObject();
		
		try {
			for ( FileSystem fs : sigar.getFileSystemList() )
			{
				if ( fs.getType() == FileSystem.TYPE_LOCAL_DISK )
				{
					String fsName = fs.getDevName();
				  	JSONArray jArray = new JSONArray();
				  	JSONObject jObj = JSONObject.fromObject(fs);
				  	FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
				  	JSONObject diskUsageObj = JSONObject.fromObject(usage);
				  	jObj.put("usage", diskUsageObj);
				  	jArray.add(jObj);
			        fileSystemInfo.put(fsName, jArray);
				}
			}
			
		} catch (SigarException e) {
			e.printStackTrace();
		} finally {
			sigar.close();
		}
		
		String tgrString = fileSystemInfo.toString().replaceAll("\\\\", "");
		return  Archiving.send(tgrString);
	}

}
